Libraries & Data
For creating this chart, we will need to load the following libraries:
- matplotlib for plotting the chart
geopandasandgeoplot: for spatial data plotting- pandas for loading the data
- pypalettes: for the color palette
- highlight_text for the annotations
import folium
from folium import plugins
import pandas as pdDataset
Let's start by loading shape data:
url = "https://raw.githubusercontent.com/holtzy/The-Python-Graph-Gallery/master/static/data/earthquakes.csv"
df = pd.read_csv(url)
df = df[df['Depth (km)']>=0.01] # depth of at least 10 meters
df.sort_values(by='Depth (km)', ascending=False, inplace=True)
df.head()| Date | Time (utc) | Region | Magnitude | Depth (km) | Latitude | Longitude | Mode | Map | year | |
|---|---|---|---|---|---|---|---|---|---|---|
| 7961 | 20/02/2019 | 06:50:47 | Banda Sea | 5.0 | 2026 | -6.89 | 129.15 | A | - | 2019.0 |
| 6813 | 07/07/2019 | 07:50:53 | Eastern New Guinea Reg, P.N.G. | 5.4 | 1010 | -5.96 | 147.90 | A | - | 2019.0 |
| 8293 | 17/01/2019 | 14:01:50 | Fiji Islands | 4.7 | 689 | -18.65 | 179.44 | A | - | 2019.0 |
| 11258 | 03/01/2018 | 06:42:58 | Fiji Islands Region | 5.5 | 677 | -19.93 | -178.89 | A | - | 2018.0 |
| 9530 | 06/09/2018 | 18:22:24 | Fiji Islands Region | 5.8 | 672 | -18.88 | 179.30 | A | - | 2018.0 |
Custom bubble style
The main steps to create a bubble map with folium are:
- initiate the map with
folium.Map() - iterate over each rows to add each individual point with
folium.CircleMarker()
In order to make each point better looking, in the CircleMarker(), we:
- change
radiusto the value inrow['Depth (km)'] - change the
colordepending on the magnitude with:color = '#0a9396' if row['Magnitude'] < 4 else '#ee9b00' if row['Magnitude'] < 6 else '#ae2012' - change the opacity with
fill_opacity - change border width with
weight
# Initialize map
m = folium.Map(
location=[0, 0], # center around Africa
zoom_start=2, # dezoom
tiles='cartodb positron' # background style
)
# Add all the individual earthquakes to the map
for idx, row in df.iterrows():
color = '#0a9396' if row['Magnitude'] < 4 else '#ee9b00' if row['Magnitude'] < 6 else '#ae2012'
folium.CircleMarker(
location=[row['Latitude'], row['Longitude']],
radius=row['Depth (km)'] * 0.03,
color=color,
fill=True,
fill_color=color,
fill_opacity=0.5,
weight=1
).add_to(m)
m
Tooltip
An interactive map without a tooltip wouldn't be very effective!
Luckily, with the tooltip argument, customizing it is straightforward, and we can even incorporate some HTML into it.
For instance, we can display the Location, Magnitude, Depth, and Date, wrap them in <b> tags for boldness, and set them as level 5 headings (<h5>).
# Initialize map
m = folium.Map(
location=[0, 0],
zoom_start=2,
tiles='cartodb positron'
)
# Add all the individual earthquakes to the map
for idx, row in df.iterrows():
tooltip_text = f"""
<h5><b>Location:</b> {row['Region']}</h5>
<h5><b>Magnitude:</b> {row['Magnitude']}</h5>
<h5><b>Depth:</b> {row['Depth (km)']} km</h5>
<h5><b>Date:</b> {row['Date']}</h5>
"""
color = '#0a9396' if row['Magnitude'] < 4 else '#ee9b00' if row['Magnitude'] < 6 else '#ae2012'
folium.CircleMarker(
location=[row['Latitude'], row['Longitude']],
radius=row['Depth (km)'] * 0.05,
color=color,
fill=True,
fill_color=color,
fill_opacity=0.5,
weight=1,
tooltip=folium.Tooltip(tooltip_text, sticky=True)
).add_to(m)
m
